package com.study.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.study.app.domain.AppVersion;
import com.study.app.mapper.AppVersionMapper;
import com.study.app.service.IAppVersionService;
import com.study.common.core.domain.PageQuery;
import com.study.common.core.page.TableDataInfo;
import com.study.common.utils.StringUtils;
import com.study.app.domain.bo.AppVersionBo;
import com.study.app.domain.vo.AppVersionVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * app版本管理Service业务层处理
 *
 * @author study
 * @date 2023-09-23
 */
@RequiredArgsConstructor
@Service
public class AppVersionServiceImpl implements IAppVersionService {

    private final AppVersionMapper baseMapper;

    /**
     * 查询app版本管理
     */
    @Override
    public AppVersionVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询app版本管理列表
     */
    @Override
    public TableDataInfo<AppVersionVo> queryPageList(AppVersionBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppVersion> lqw = buildQueryWrapper(bo);
        Page<AppVersionVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询app版本管理列表
     */
    @Override
    public List<AppVersionVo> queryList(AppVersionBo bo) {
        LambdaQueryWrapper<AppVersion> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppVersion> buildQueryWrapper(AppVersionBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppVersion> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPhoneType() != null, AppVersion::getPhoneType, bo.getPhoneType());
        lqw.like(StringUtils.isNotBlank(bo.getVersionName()), AppVersion::getVersionName, bo.getVersionName());
        lqw.eq(bo.getVersionNumber() != null, AppVersion::getVersionNumber, bo.getVersionNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getVersionDescription()), AppVersion::getVersionDescription, bo.getVersionDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getVersionUrl()), AppVersion::getVersionUrl, bo.getVersionUrl());
        lqw.eq(bo.getIsForce() != null, AppVersion::getIsForce, bo.getIsForce());
        lqw.eq(bo.getIsGrounding() != null, AppVersion::getIsGrounding, bo.getIsGrounding());
        lqw.eq(bo.getModifiedTime() != null, AppVersion::getModifiedTime, bo.getModifiedTime());
        lqw.orderByDesc(AppVersion::getCreateTime);
        return lqw;
    }

    /**
     * 新增app版本管理
     */
    @Override
    public Boolean insertByBo(AppVersionBo bo) {
        AppVersion add = BeanUtil.toBean(bo, AppVersion.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改app版本管理
     */
    @Override
    public Boolean updateByBo(AppVersionBo bo) {
        AppVersion update = BeanUtil.toBean(bo, AppVersion.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppVersion entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除app版本管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean checkVersion(Integer phoneType, Integer versionNumber) {

        //获取当前手机类型最新的版本号
        List<AppVersion> appVersions = baseMapper.selectList(new LambdaQueryWrapper<AppVersion>()
                .eq(AppVersion::getPhoneType, phoneType)
                .eq(AppVersion::getIsGrounding, 2)
                .orderByDesc(AppVersion::getVersionNumber));

        if (appVersions.isEmpty()){
            return false;
        }

        return Integer.parseInt(appVersions.get(0).getVersionNumber()) > versionNumber ? true : false;
    }


}
